<?php


namespace app\model;
use think\facade\Db;
use think\Model;
use app\model\SizeModel;
class VehicleModel extends Model
{
    protected $table = "store_vehicle";

    /**
     * @param $param
     * @return SizeModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 尺寸列表
     * @author: dongdong
     * @date: 2025/1/7 15:40
     */
    public static function lists($param){
        $where = [];
        $where['status'] = 1;
        if($param['transport_way']){
            $where['transport_way'] = $param['transport_way'];
        }
        return self::where($where)->field("id,title,title_ru")->select();
    }


    /**
     * @param $param
     * @return array|\think\Collection|Db[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 尺寸载具组合
     * @author: dongdong
     * @date: 2025/1/7 16:05
     */
    public static function size_vehicle($param){
        $where = [];
        if($param['size_id']){
            $where['size_id'] = $param['size_id'];
        }
        if($param['vehicle_id']){
            $where['vehicle_id'] = $param['vehicle_id'];
        }
        $SizeModel = new SizeModel();
        $list = Db::table("store_size_vehicle")->field("size_id,vehicle_id")->where($where)->select();
        $arr = [];
        $str = [];
        foreach ($list as $k =>&$v){
            array_push($arr,$v);
        }
        foreach ($arr as $k =>&$v){
            $arr[$k]['size'] = $SizeModel::where(["id"=>$v['size_id']])->field("id,title,title_ru")->find();
            $arr[$k]['vehicle'] = self::where(["id"=>$v['vehicle_id']])->field("id,title,title_ru")->find();
        }

        if($param['size_id']){
            foreach ($arr as $k =>&$v){
                array_push($str,$v['vehicle']);
            }
        }else{
            foreach ($arr as $k =>&$v){
                array_push($str,$v['size']);
            }
        }
        return  $str;
    }

    /**
     * @param $id
     * @return VehicleModel|array|mixed|Model|null
     * @notes 载具详情
     * @author: dongdong
     * @date: 2025/1/8 14:43
     */
    public function info($id){
        try {
            return self::where("id=$id")->field("id,title,title_ru")->find();
        }catch (\Exception $e){
            return [];
        }
    }


}